package com.platform.web.controller.base;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.base.domain.*;
import com.platform.base.service.IBaseLocationService;
import com.platform.base.service.ICollectorInfoService;
import com.platform.base.service.IMeterTypeService;
import com.platform.common.core.text.Convert;
import com.platform.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.platform.common.annotation.Log;
import com.platform.common.enums.BusinessType;
import com.platform.base.service.IMeterInfoService;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.utils.poi.ExcelUtil;
import com.platform.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 热信息Controller
 * 
 * @author wangqi
 * @date 2020-09-26
 */
@Controller
@RequestMapping("/base/meter/info")
public class MeterInfoController extends BaseController
{
    private String prefix = "base/meter/info";

    @Autowired
    private IMeterInfoService meterInfoService;

    @Autowired
    private IMeterTypeService meterTypeService;

    @Autowired
    private IBaseLocationService baseLocationService;

    @Autowired
    private ICollectorInfoService collectorInfoService;

    @RequiresPermissions("base:meter:info:view")
    @GetMapping(value = { "", "/{typeId}" })
    public String info(@PathVariable(value = "typeId", required = false) String typeId, ModelMap mmap)
    {
        List<MeterType> types = meterTypeService.list();
        mmap.put("types", types);
        if(typeId==null){
            typeId = "";
        }
        mmap.put("typeId", typeId);
        return prefix + "/info";
    }

    /**
     * 根据采集器查询热表信息
     * @param collectorId
     * @param mmap
     * @return
     */
    @RequiresPermissions("base:meter:info:view")
    @GetMapping("/collector/{collectorId}")
    public String collector(@PathVariable(value = "collectorId") String collectorId, ModelMap mmap)
    {
        List<MeterType> types = meterTypeService.list();
        mmap.put("types", types);
        mmap.put("typeId", "");
        mmap.put("collectorId", collectorId);
        return prefix + "/info";
    }

    /**
     * 查询热信息列表
     */
    @RequiresPermissions("base:meter:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MeterInfo meterInfo)
    {
        startPage();
        List<MeterInfo> list = meterInfoService.selectMeterInfoList(meterInfo);
        return getDataTable(list);
    }
    /**
     * 查询热信息列表
     */
    @RequiresPermissions("base:meter:info:locationlist")
    @PostMapping("/locationlist")
    @ResponseBody
    public TableDataInfo locationlist(String locationId)
    {
        startPage();
        List<MeterInfo> locationlist = meterInfoService.selectMeterInfoBylocationId(locationId);
        return getDataTable(locationlist);
    }

    /**
     * @Author 王琦
     * @Description //热表信息导入
     * @Date 2020/6/19 19:56
     * @Param [file, updateSupport]
     * @return com.platform.common.core.domain.AjaxResult
     */
    @Log(title = "热表信息导入", businessType = BusinessType.IMPORT)
    @RequiresPermissions("base:meter:info:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<MeterInfo> util = new ExcelUtil<MeterInfo>(MeterInfo.class);
        List<MeterInfo> infoList = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = meterInfoService.importInfo(infoList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * @Author 王琦
     * @Description //导入模板
     * @Date 2020/6/19 19:56
     * @Param []
     * @return com.platform.common.core.domain.AjaxResult
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<MeterInfo> util = new ExcelUtil<MeterInfo>(MeterInfo.class);
        return util.importTemplateExcel("热表信息");
    }

    /**
     * 导出热信息列表
     */
    @RequiresPermissions("base:meter:info:export")
    @Log(title = "热信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MeterInfo meterInfo)
    {
        List<MeterInfo> list = meterInfoService.selectMeterInfoList(meterInfo);
        ExcelUtil<MeterInfo> util = new ExcelUtil<MeterInfo>(MeterInfo.class);
        return util.exportExcel(list, "热表信息");
    }

    /**
     * 新增热信息
     */
    @GetMapping("/add/{locationId}/{typeId}/{collectorId}")
    public String add(@PathVariable(value = "locationId") String locationId, @PathVariable(value = "typeId") String typeId, @PathVariable(value = "collectorId") String collectorId, ModelMap mmap)
    {
        List<MeterType> types = meterTypeService.list();
        mmap.put("types", types);
        mmap.put("typeId", typeId);
        mmap.put("collectorId", collectorId);
        List<CollectorInfo> infos = collectorInfoService.list();
        mmap.put("infos", infos);
        mmap.put("location", baseLocationService.getById(locationId));
        return prefix + "/add";
    }

    /**
     * 新增保存热信息
     */
    @RequiresPermissions("base:meter:info:add")
    @Log(title = "热信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated MeterInfo meterInfo)
    {
        meterInfo.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(meterInfoService.insertMeterInfo(meterInfo));
    }

    /**
     * 修改热信息
     */
    @GetMapping("/edit/{meterId}")
    public String edit(@PathVariable("meterId") String meterId, ModelMap mmap)
    {
        MeterInfo meterInfo = meterInfoService.selectMeterInfoById(meterId);
        mmap.put("meterInfo", meterInfo);
        List<MeterType> types = meterTypeService.list();
        mmap.put("types", types);
        List<CollectorInfo> infos = collectorInfoService.list();
        mmap.put("infos", infos);
        return prefix + "/edit";
    }

    /**
     * 修改保存热信息
     */
    @RequiresPermissions("base:meter:info:edit")
    @Log(title = "热信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated MeterInfo meterInfo)
    {
        meterInfo.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(meterInfoService.updateMeterInfo(meterInfo));
    }

    /**
     * 删除热信息
     */
    @RequiresPermissions("base:meter:info:remove")
    @Log(title = "热信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(meterInfoService.deleteMeterInfoByIds(ids));
    }

    /**
     * 校验阀门信息是否唯一
     */
    @PostMapping("/checkMeterUnique")
    @ResponseBody
    public String checkMeterUnique(MeterInfo meterInfo)
    {
        return meterInfoService.checkMeterUnique(meterInfo);
    }



    /**
     * 根据地址查询热表信息
     */
    @PostMapping(value = { "/selectMeterByLocation/{locationId}", "/selectMeterByLocation/{meterId}/{locationId}" })
    @ResponseBody
    public String selectMeterByLocation(@PathVariable(value = "meterId", required = false) String meterId, @PathVariable("locationId") Long locationId)
    {
        MeterInfo meterInfo = meterInfoService.getOne(new QueryWrapper<MeterInfo>().eq("location_id", locationId));
        if(meterInfo!=null){
            if(meterId!=null&&meterId.equals(meterInfo.getMeterId())){
                return "";
            }
            return meterInfo.getMeterId();
        }else{
            return "";
        }
    }

    /**
     * 热表登记
     */
    @Log(title = "热表登记", businessType = BusinessType.OTHER)
    @RequiresPermissions("base:meter:info:register")
    @PostMapping("/register/{meterIds}")
    @ResponseBody
    public AjaxResult register(@PathVariable("meterIds") String meterIds) throws InterruptedException {
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = meterInfoService.registerInfo(Convert.toStrArray(meterIds), operName);
        return AjaxResult.success(message);
    }

    /**
     * 热表注销
     */
    @Log(title = "热表注销", businessType = BusinessType.OTHER)
    @RequiresPermissions("base:meter:info:cancel")
    @PostMapping("/cancel/{meterIds}")
    @ResponseBody
    public AjaxResult cancel(@PathVariable("meterIds") String meterIds) throws InterruptedException {
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = meterInfoService.cancelInfo(Convert.toStrArray(meterIds), operName);
        return AjaxResult.success(message);
    }

    /**
     * 热表数据采集
     */
    @Log(title = "热表数据采集", businessType = BusinessType.OTHER)
    @RequiresPermissions("base:meter:info:check")
    @PostMapping("/check/{meterIds}")
    @ResponseBody
    public AjaxResult check(@PathVariable("meterIds") String meterIds) throws InterruptedException {
        String message = meterInfoService.checkInfo(Convert.toStrArray(meterIds));
        return AjaxResult.success(message);
    }
}
